$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tbUser/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'userId', index: 'user_id', width: 50, key: true },
			// { label: '用户头像', name: 'headPicture', index: 'head_picture', width: 80 },
			{ label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
			{ label: '电话号码(账号)', name: 'mobile', index: 'mobile', width: 80 },
			{ label: '用户钱包(余额)', name: 'wallet', index: 'wallet', width: 80 },
			// { label: '登录密码', name: 'password', index: 'password', width: 80 },
			{ label: '积分', name: 'integral', index: 'integral', width: 80 },
			// { label: '真实姓名', name: 'realname', index: 'realname', width: 80 },
			// { label: '个性签名', name: 'signature', index: 'signature', width: 80 },
			// { label: '选择城市', name: 'city', index: 'city', width: 80 },
			// { label: '订单消息编码', name: 'orderNewsCode', index: 'order_news_code', width: 80 },
			// { label: '系统消息编码', name: 'systemNewsCode', index: 'system_news_code', width: 80 },
			// { label: '资产消息编码', name: 'assetNewsCode', index: 'asset_news_code', width: 80 },
			// { label: '出生日期', name: 'birthday', index: 'birthday', width: 80 },
			{ label: '性别', name: 'sex', index: 'sex', width: 80
				,
                formatter: function (value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-success">女性</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-primary">男性</span>';
                    }else{
                        return '<span class="label label-primary">未知</span>';
					}
                }
			},
			// { label: '背景图片', name: 'backgroundPicture', index: 'background_picture', width: 80 },
			{ label: '删除状态', name: 'deleteType', index: 'delete_type', width: 80 ,
                formatter: function (value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-success">未删除</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-primary">已删除</span>';
                    }
                }},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }
			// { label: '是否技师，0:否，1:是', name: 'isTechnician', index: 'is_technician', width: 80 },
			// { label: '用户类型 1用户 2技师 3店铺', name: 'userType', index: 'user_type', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            nickname : null
        },
		user: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.user.userId == null ? "sys/tbUser/save" : "sys/tbUser/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/tbUser/delete",
				    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userId){
			$.get(baseURL + "sys/tbUser/info/"+userId, function(r){
                vm.user = r.user;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'nickname': vm.q.nickname
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});